<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--科技人员数据-->
      <el-col :span="20" :xs="24">
        <!-- 顶部搜索 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="姓名" prop="name">
            <el-input
              v-model="queryParams.name"
              style="background-color: white !important;"
              placeholder="请输入姓名"
              clearable
              @keyup.enter.native="handleQuery"
              size="mini"
            />
          </el-form-item>
          <el-form-item label="熟悉领域" prop="sxLy">
            <el-select v-model="queryParams.sxLy" placeholder="请选择熟悉领域" clearable size="mini">
              <el-option
                v-for="dict in dict.type.rcpt_sxly"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="人员分类" prop="ryFl">
            <el-select v-model="queryParams.ryFl" placeholder="请选择人员分类" clearable size="mini">
              <el-option
                v-for="dict in dict.type.rcpt_ryfl"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否专家" prop="sfZj">
            <el-select v-model="queryParams.sfZj" placeholder="请选择是否专家" clearable size="mini">
              <el-option
                v-for="dict in dict.type.rcpt_sf"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 顶部按钮 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              :disabled="!selectedRow || (selectedRow.processStatus !== 1 && selectedRow.processStatus !== 4)"
              @click="handleUpdate"
              v-hasPermi="['kjry:kjry:edit']"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['kjry:kjry:export']"
            >导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 列表 -->
        <el-table @row-click="clickRow" ref="table"
                  highlight-current-row :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                  v-loading="loading" :data="kjryList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="序号" align="center" type="index" width="50"/>
          <el-table-column label="姓名" align="center" prop="name" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button
                type="text"
                @click="handleButtonClick(scope.row)"
              >
                <el-link type="primary">{{ scope.row.name }}</el-link>
              </el-button>
            </template>
          </el-table-column>
<!--          <el-table-column label="性别" align="center" prop="sex" show-overflow-tooltip>-->
<!--            <template slot-scope="scope">-->
<!--              <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <!--      <el-table-column label="出生日期" align="center" prop="csrq" width="120" show-overflow-tooltip>-->
          <!--        <template slot-scope="scope">-->
          <!--          <span>{{ parseTime(scope.row.csrq, '{y}-{m}-{d}') }}</span>-->
          <!--        </template>-->
          <!--      </el-table-column>-->
          <el-table-column label="年龄" align="center" prop="age"/>
          <el-table-column label="人员分类" align="center" prop="ryFl" show-overflow-tooltip width="120">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.rcpt_ryfl" :value="scope.row.ryFl"/>
            </template>
          </el-table-column>
          <el-table-column label="是否专家" align="center" prop="sfZj" show-overflow-tooltip>
            <template slot-scope="scope">
              <dict-tag :options="dict.type.rcpt_sf" :value="scope.row.sfZj"/>
            </template>
          </el-table-column>
          <el-table-column label="熟悉领域" align="center" prop="sxLy" show-overflow-tooltip>
            <template slot-scope="scope">
              <dict-tag :options="dict.type.rcpt_sxly" :value="scope.row.sxLy"/>
            </template>
          </el-table-column>
          <el-table-column label="所学专业" align="center" prop="sxZy" show-overflow-tooltip/>
          <el-table-column label="毕业院校" align="center" prop="byYx" show-overflow-tooltip/>
          <el-table-column label="职务" align="center" prop="zw" show-overflow-tooltip/>
          <el-table-column label="职称" align="center" prop="zc" width="120">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.rcpt_zc" :value="scope.row.zc"/>
            </template>
          </el-table-column>
<!--          <el-table-column label="电话" align="center" prop="phone" show-overflow-tooltip/>-->
<!--          <el-table-column label="QQ" align="center" prop="qq" show-overflow-tooltip/>-->
          <!--      <el-table-column label="是否在职" align="center" prop="sfZz" show-overflow-tooltip>-->
          <!--        <template slot-scope="scope">-->
          <!--          <dict-tag :options="dict.type.rcpt_sf" :value="scope.row.sfZz"/>-->
          <!--        </template>-->
          <!--      </el-table-column>-->
          <el-table-column label="所属单位" align="center" prop="ssDw" show-overflow-tooltip/>
<!--          <el-table-column label="证件照" align="center" prop="zjZ" width="100" show-overflow-tooltip>-->
<!--            <template slot-scope="scope">-->
<!--              <image-preview :src="scope.row.zjZ" :width="50" :height="50" show-overflow-tooltip/>-->
<!--            </template>-->
<!--          </el-table-column>-->
          <el-table-column label="状态" align="center" prop="processStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-if="scope.row.processStatus===1 || scope.row.processStatus===4"
                v-hasPermi="['kjry:kjry:edit']"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleJinDu(scope.row)"
                v-if="scope.row.processStatus !== 1"
              >流程状态
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 列表分页 -->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改科技人员对话框 -->
        <vxe-modal :title="title" v-model="open" width="60%" append-to-body showFooter esc-closable show-zoom resize>
          <el-form ref="formDialog" :model="form" :rules="rules" label-width="100px">
            <el-tabs type="border-card" v-model="activeName">
              <!-- 基本资料 -->
              <el-tab-pane name="jbZl" :lazy=true>
                <span slot="label"><i class="el-icon-date"></i> 基本资料</span>
                <div style="height: 70vh; overflow-y: auto;">
                  <el-card shadow="never">
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="姓名：" prop="name">
                          <el-input v-model="form.name" placeholder="请输入姓名"/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="性别：" prop="sex">
                          <el-select v-model="form.sex" placeholder="请选择性别">
                            <el-option
                              v-for="dict in dict.type.sys_user_sex"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="身份证号：" prop="sfzh">
                          <el-input v-model="form.sfzh" placeholder="请输入身份证号"/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="出生日期：" prop="csrq">
                          <el-date-picker clearable
                                          v-model="form.csrq"
                                          type="date"
                                          style="width: 100%;"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择出生日期">
                          </el-date-picker>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="职称：" prop="zc">
                          <el-select v-model="form.zc" placeholder="请选择职称">
                            <el-option
                              v-for="dict in dict.type.rcpt_zc"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="职务：" prop="zw">
                          <el-input v-model="form.zw" placeholder="请输入职务"/>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="学历：" prop="xl">
                          <el-input v-model="form.xl" placeholder="请输入学历"/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="毕业院校：" prop="byYx">
                          <el-input v-model="form.byYx" placeholder="请输入毕业院校"/>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="所学专业：" prop="sxZy">
                          <el-input v-model="form.sxZy" placeholder="请输入所学专业"/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="熟悉领域：" prop="sxLy">
                          <el-select v-model="form.sxLy" placeholder="请选择熟悉领域">
                            <el-option
                              v-for="dict in dict.type.rcpt_sxly"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="人员分类：" prop="ryFl">
                          <el-select v-model="form.ryFl" placeholder="请选择人员分类">
                            <el-option
                              v-for="dict in dict.type.rcpt_ryfl"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="是否专家：" prop="sfZj">
                          <el-select v-model="form.sfZj" placeholder="请选择是否专家">
                            <el-option
                              v-for="dict in dict.type.rcpt_sf"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="电话：" prop="phone">
                          <el-input v-model="form.phone" placeholder="请输入电话"/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="QQ：" prop="qq">
                          <el-input v-model="form.qq" placeholder="请输入QQ"/>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-row :gutter="24">
                      <el-col :span="12">
                        <el-form-item label="是否在职：" prop="sfZz">
                          <el-select v-model="form.sfZz" placeholder="请选择是否在职">
                            <el-option
                              v-for="dict in dict.type.rcpt_sf"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="所属单位：" prop="ssDw">
                          <el-input v-model="form.ssDw" placeholder="请输入所属单位" @focus="openDeptTree"/>
                          <!--                      <el-input v-model="form.ssDw" placeholder="请输入所属单位" />-->
                        </el-form-item>
                      </el-col>
                    </el-row>
                    <el-form-item label="专业特长：" prop="zyTc">
                      <el-input :rows="5" v-model="form.zyTc" type="textarea" placeholder="请输入内容"/>
                    </el-form-item>
                    <el-row :gutter="24">
                      <el-col :span="15">
                        <el-form-item label="个人简介：" prop="grJj">
                          <el-input :rows="9" v-model="form.grJj" type="textarea" placeholder="请输入内容"/>
                        </el-form-item>
                      </el-col>
                      <el-col :span="8">
                        <el-form-item label="证件照上传：" prop="zjZ">
                          <image-upload v-model="form.zjZ"/>
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </el-card>
                </div>
              </el-tab-pane>
              <!-- 科技人员工作履历信息 -->
              <el-tab-pane name="gzLv" :lazy=true>
                <span slot="label"><i class="el-icon-date"></i> 科技人员工作履历信息</span>
                <div style="height: 70vh; overflow-y: auto;">
                  <el-row :gutter="10" class="mb8">

                    <el-col :span="1.5">
                      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddKjryGzlv">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                      <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteKjryGzlv">删除
                      </el-button>
                    </el-col>
                  </el-row>
                  <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                            :data="kjryGzlvList" :row-class-name="rowKjryGzlvIndex"
                            @selection-change="handleKjryGzlvSelectionChange" ref="kjryGzlv">
                    <el-table-column type="selection" width="50" align="center"/>
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
                    <el-table-column label="开始时间" align="center" prop="ksTime" width="170">
                      <template slot-scope="scope">
                        <el-date-picker style="width: 90%" clearable v-model="scope.row.ksTime" type="date"
                                        value-format="yyyy-MM-dd" placeholder="开始时间"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="截止时间" align="center" prop="jzTime" width="170">
                      <template slot-scope="scope">
                        <el-date-picker style="width: 90%" clearable v-model="scope.row.jzTime" type="date"
                                        value-format="yyyy-MM-dd" placeholder="截止时间"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="单位/部门" align="center" prop="dwBm">
                      <template slot-scope="scope">
                        <el-input v-model="scope.row.dwBm" placeholder="请输入单位/部门"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="职务" align="center" prop="zw">
                      <template slot-scope="scope">
                        <el-input v-model="scope.row.zw" placeholder="请输入职务"/>
                      </template>
                    </el-table-column>
                    <el-table-column label="负责内容" align="center" prop="fzNr">
                      <template slot-scope="scope">
                        <el-input v-model="scope.row.fzNr" placeholder="请输入负责内容"/>
                      </template>
                    </el-table-column>
                  </el-table>

                </div>
              </el-tab-pane>
              <!-- 附件资料 -->
              <el-tab-pane name="fjZl" :lazy=true>
                <span slot="label"><i class="el-icon-date"></i> 附件资料</span>
                <div style="height: 70vh; overflow-y: auto;">
                  <el-row :gutter="10" class="mb8">
                    <el-col :span="1.5">
                      <!-- 上传按钮 -->
                      <el-button size="mini" icon="el-icon-upload" type="primary" @click="kjryFjShangChuan">上传</el-button>
                    </el-col>
                    <el-col :span="1.5">
                      <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteKjryFj">删除</el-button>
                    </el-col>
                  </el-row>
                  <el-table :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
                            :data="kjryFjList" :row-class-name="rowKjryFjIndex"
                            @selection-change="handleKjryFjSelectionChange"
                            ref="kjryFj">
                    <el-table-column type="selection" width="50" align="center"/>
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
                    <el-table-column label="文件名称" show-overflow-tooltip align="center" prop="fileName"
                                     :formatter="getFileNames"/>
                    <el-table-column label="文件大小" align="center" prop="fileSize" width="130"/>
                    <el-table-column label="上传时间" align="center" prop="scTime" width="110"/>
                    <el-table-column label="上传人" align="center" prop="scUser" width="130"/>
                    <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
                      <template slot-scope="scope">
                        <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-edit"
                          @click="handleDownload(scope.row)"
                        >下载
                        </el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </div>
              </el-tab-pane>
            </el-tabs>
          </el-form>
          <div slot="footer" class="dialog-footer dialog-footer-center">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </vxe-modal>

        <!-- 选择部门弹窗组件 -->
        <dept-tree ref="deptOpen"/>

        <!-- 查看流程状态 -->
        <JinDu ref="picture"/>

        <!-- 科技人员附件上传弹窗 -->
        <vxe-modal title="附件上传" v-model="fjShangChuan" width="25%" append-to-body @close="fjClose" showFooter>
          <el-upload
            align="center"
            class="upload-demo"
            multiple
            :action="uploadFileUrl"
            :before-upload="handleBeforeUpload"
            :file-list="fileList"
            :limit="20"
            :on-error="handleUploadError"
            :on-exceed="handleExceed"
            :on-success="handleUploadSuccess"
            :headers="headers"
            ref="fileUpload"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">
              请上传
              <template> 大小不超过 <b style="color: #f56c6c">{{ 50 }}MB</b></template>
              <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b></template>
              的文件
            </div>
          </el-upload>

          <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
            <el-button v-if="allFilesUploaded === false" size="mini" type="primary" @click="fjShangChuan = false">关 闭
            </el-button>
            <el-button v-if="allFilesUploaded === true" size="mini" type="primary" @click="fjShangChuan = false">上传完成, 关闭
            </el-button>
          </div>

        </vxe-modal>

        <!-- 点击姓名查看科研人员详情 -->
        <kjry-xq ref="kjryXq"/>

      </el-col>
    </el-row>

  </div>
</template>

<script>
import {deptTreeSelect} from "@/api/system/user";
import {
  addKjry,
  getKjry,
  insertKjryFjByxmsbId,
  listKjry,
  selectKjryFjList,
  selectKjryGzlvByKjryId,
  updateKjry
} from "@/api/kjry/kjry";
import {getToken} from "@/utils/auth";
import JinDu from "@/views/components/liuChengState/jindu";
import KjryXq from "@/views/kjry/kjry/components/kjryXq";
import DeptTree from "@/views/components/dept/deptTree";

export default {
  name: "Query", //科技人员查询页面
  dicts: ['rcpt_sxly', 'rcpt_ryfl', 'sys_user_sex', 'rcpt_sf', 'process_business_status', 'rcpt_zc'],
  components: {
    DeptTree,
    KjryXq,
    JinDu
  },
  provide() {
    return {
      updateDeptLabel: (val) => {
        this.receivedDeptLabel = val;
        this.form.ssDw = this.receivedDeptLabel;
      },
      updateDeptId:  (val) => {
        this.receivedDeptId = val;
        this.form.deptId = this.receivedDeptId;
      },
    };
  },
  data() {
    return {
      // 部门名称
      deptName: null,
      // 部门树选项
      deptOptions: null,
      // 表单参数
      defaultProps: {
        children: "children",
        label: "label"
      },
      receivedDeptId: null,
      receivedDeptLabel: null,
      activeName: 'jbZl', //默认打开的标签
      selectedRow: null, // 保存当前选中的行
      historyList: [], //历史信息
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjShangChuan: false, //附件上传弹窗
      kjryIdEdit: "", //要修改的 科技人员id
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      getKjryId: null, //从后端获得的科技人员主键UUID
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedKjryFj: [],
      // 工作履历子表选中数据
      checkedKjryGzlv: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 科技人员表格数据
      kjryList: [],
      // 科技人员附件表格数据
      kjryFjList: [],
      // 科技人员工作履历表格数据
      kjryGzlvList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sxLy: null,
        ryFl: this.$route.query.ryFl,
        sfZj: null,
        userId: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{required: true, message: "姓名不能为空", trigger: "blur"}],
        sex: [{required: true, message: "性别不能为空", trigger: "change"}],
        sfzh: [{required: true, message: "身份证号不能为空", trigger: "blur"},
          /*{ pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: "身份证号格式不正确", trigger: "blur" }*/],
        csrq: [{required: true, message: "出生日期不能为空", trigger: "blur"}],
        zc: [{required: true, message: "职称不能为空", trigger: "blur"}],
        zw: [{required: true, message: "职务不能为空", trigger: "blur"}],
        xl: [{required: true, message: "学历不能为空", trigger: "blur"}],
        byYx: [{required: true, message: "毕业院校不能为空", trigger: "blur"}],
        sxZy: [{required: true, message: "所学专业不能为空", trigger: "blur"}],
        sxLy: [{required: true, message: "熟悉领域不能为空", trigger: "change"}],
        ryFl: [{required: true, message: "人员分类不能为空", trigger: "change"}],
        sfZj: [{required: true, message: "是否专家不能为空", trigger: "change"}],
        phone: [{required: true, message: "电话不能为空", trigger: "blur"}],
        sfZz: [{required: true, message: "是否在职不能为空", trigger: "change"}],
        ssDw: [{required: true, message: "所属单位不能为空", trigger: "blur"}],
        zyTc: [{required: true, message: "专业特长不能为空", trigger: "blur"}],
        grJj: [{required: true, message: "个人简介不能为空", trigger: "blur"}],
      },
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        console.log(response, 'adsadsads')
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },

    //打开选择部门弹窗组件
    openDeptTree() {
      this.$refs.deptOpen.handleOpen();
    },
    //查询人员详情
    handleButtonClick(row) {
      this.$refs.kjryXq.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;
      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    // 文件下载处理
    handleDownload(row) {
      const resource = row.fileName;
      // 默认方法
      this.$download.resource(resource);
    },
    // 获取文件名原名
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    //关闭上传附件弹窗后
    fjClose() {
      this.fileList = []
      this.allFilesUploaded = false
    },
    //上传文件之前的钩子
    handleBeforeUpload(file) {
      let that = this;
      // 校检文件类型
      if (this.fileType) {
        const fileName = file.name.split('.');
        const fileExt = fileName[fileName.length - 1];
        const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
        if (!isTypeOk) {
          this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
          return false;
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        this.$modal.loading("正在上传文件，请稍候...");
        this.number++;
        return true;
      }
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        strs += list[i].url + separator;
      }
      return strs !== '' ? strs.substr(0, strs.length - 1) : '';
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功
    handleUploadSuccess(res, file) {
      let that = this;
      if (res.code === 200) {
        this.uploadList.push({name: res.fileName, url: res.fileName, size: that.formatFileSize(file.size)});
        let xzItemFile = {
          "kjryId": that.getKjryId,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        let xgItemFile = {
          "kjryId": that.kjryIdEdit,
          "fileName": res.fileName,
          "fileSize": that.formatFileSize(file.size)
        };
        if (this.xg === false) {
          // 点击新增按钮的上传
          insertKjryFjByxmsbId(xzItemFile).then(resFil => {
            that.getKjryFjList();
          })
        } else if (this.xg === true) {
          // 点击修改按钮的上传
          insertKjryFjByxmsbId(xgItemFile).then(resFil => {
            that.getKjryFjList();
          })
        }
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
      this.allFilesUploaded = true
      this.$modal.msgSuccess("上传成功")
      // this.fjShangChuan = false
    },
    getKjryFjList() {
      let that = this;
      if (this.xg === false) {
        //点击新增 上传文件后的文件列表
        selectKjryFjList(that.getKjryId).then(resFileList => {
          that.kjryFjList = resFileList.data;
        })
      } else {
        //点击修改 上传文件后的文件列表
        selectKjryFjList(that.kjryIdEdit).then(resFileList => {
          that.kjryFjList = resFileList.data;
        })
      }
    },
    // 显示文件大小为KB, MB, GB, TB
    formatFileSize(size) {
      if (size === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      const i = Math.floor(Math.log(size) / Math.log(k));
      return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.fileList = this.fileList.map(item => {
          // 使用replace方法去除name中的特定前缀
          item.name = item.name.split('/').pop();
          return item;
        });
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    kjryFjShangChuan() { //科技人员附件上传按钮
      this.fjShangChuan = true;
    },
    // 查询科技人员列表
    getList() {
      this.loading = true;
      listKjry(this.queryParams).then(response => {
        this.kjryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 新增表单重置
    reset() {
      this.form = {
        kjryId: this.getKjryId, sort: null, name: null, sex: null,
        sfzh: null, age: null, csrq: null, zc: null, zw: null, xl: null,
        byYx: null, sxZy: null, sxLy: null, ryFl: null, sfZj: null,
        phone: null, qq: null, sfZz: null, ssDw: null, zyTc: null,
        grJj: null, zjZ: null, idWhere: null, createBy: null,
        createTime: null, updateBy: null, updateTime: null, userId: null, deptId: null
      };
      this.kjryFjList = [];
      this.kjryGzlvList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.kjryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.xg = true
      this.reset();
      const kjryId = row.kjryId || this.ids
      this.kjryIdEdit = row.kjryId
      getKjry(kjryId).then(response => {
        this.form = response.data;
        this.kjryFjList = response.data.kjryFjList;
        this.activeName = 'jbZl'
        this.open = true;
        this.title = "修改科技人员";
      });
      selectKjryGzlvByKjryId(kjryId).then(res => {
        this.kjryGzlvList = res.data.kjryGzlvList;
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          this.form.kjryFjList = this.kjryFjList;
          this.form.kjryGzlvList = this.kjryGzlvList;
          if (this.form.idWhere != null) {
            updateKjry(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.xg = false
            });
          } else {
            addKjry(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 科技人员附件序号 */
    rowKjryFjIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 科技人员附件删除按钮操作 */
    handleDeleteKjryFj() {
      if (this.checkedKjryFj.length === 0) {
        this.$modal.msgError("请先选择要删除的科技人员附件数据");
      } else {
        const kjryFjList = this.kjryFjList;
        const checkedKjryFj = this.checkedKjryFj;
        this.kjryFjList = kjryFjList.filter(function (item) {
          return checkedKjryFj.indexOf(item.index) === -1
        });
      }
    },
    /** 复选框选中数据 */
    handleKjryFjSelectionChange(selection) {
      this.checkedKjryFj = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('kjry/kjry/export', {
        ...this.queryParams
      }, `kjry_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('kjry/kjry/importTemplate', {}, `template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 科技人员工作履历序号 */
    rowKjryGzlvIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 科技人员工作履历添加按钮操作 */
    handleAddKjryGzlv() {
      let obj = {};
      obj.ksTime = "";
      obj.jzTime = "";
      obj.dwBm = "";
      obj.zw = "";
      obj.fzNr = "";
      this.kjryGzlvList.push(obj);
    },
    /** 科技人员工作履历删除按钮操作 */
    handleDeleteKjryGzlv() {
      if (this.checkedKjryGzlv.length === 0) {
        this.$modal.msgError("请先选择要删除的科技人员工作履历数据");
      } else {
        const kjryGzlvList = this.kjryGzlvList;
        const checkedKjryGzlv = this.checkedKjryGzlv;
        this.kjryGzlvList = kjryGzlvList.filter(function (item) {
          return checkedKjryGzlv.indexOf(item.index) === -1
        });
      }
    },
    /** 复选框选中数据 */
    handleKjryGzlvSelectionChange(selection) {
      this.checkedKjryGzlv = selection.map(item => item.index)
    },
  },
}
</script>

<style scoped>

</style>
